/*
 *  Copyright 2012 yura.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */

package org.opu.odg.dg.util;

import java.io.ByteArrayInputStream;
import org.apache.commons.io.output.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;
/**
 *
 * @author yura
 */
public class ArchiveUtil {

    public static final int BUFFER = 4096;

    public static byte[] rewriteArchiveItem(byte[] archiveData, String name,
            ByteArrayOutputStream bout)  throws IOException {
        
        Map<String, byte[]> archive = readArchive(new ByteArrayInputStream(archiveData));

        byte toBinary[] = bout.toByteArray();

        archive.put(name, toBinary);

        bout.reset();
        ArchiveUtil.writeArchive(archive, bout);

        return bout.toByteArray();
    }

    public static Map<String, byte[]> readArchive(byte[] bytes)
            throws IOException {
        return readArchive(new ByteArrayInputStream(bytes));
    }

    /**
     * Read all data from archive and put it into map
     * @param stream input stream of archive
     * @return map with content of archive, where key is the name of entry in
     * archive and valu is the entry value
     * @throws IOException if an I/O error has occurred
     */
    public static Map<String, byte[]> readArchive(InputStream stream)
            throws IOException {

        ZipInputStream zis = new ZipInputStream(stream);
        Map<String, byte[]> o_data = new HashMap<String, byte[]>();
        ZipEntry ze;

        while ((ze = zis.getNextEntry()) != null) {
            String name = ze.getName();
            byte data[] = new byte[BUFFER];
            ByteArrayOutputStream dest = new ByteArrayOutputStream();

            int count = 0;
            while ((count = zis.read(data, 0, BUFFER)) != -1) {
                dest.write(data, 0, count);
            }

            o_data.put(name, dest.toByteArray());
            zis.closeEntry();
        }

        zis.close();
        stream.close();
        return o_data;
    }

    /**
     * Represent map content as a zip archive and write it into output stream
     * @param data content
     * @param bout where to zip archive need to write
     * @throws IOException if an I/O error has occurred
     */
    public static void writeArchive(Map<String, byte[]> data, OutputStream bout)
            throws IOException {
        ZipOutputStream out = new ZipOutputStream(bout);

        for (String name : data.keySet()) {
            out.putNextEntry(new ZipEntry(name));
            out.write(data.get(name));
        }

        out.close();
        bout.close();
    }
}
